<?php
/**
 * @user: thanatos <thanatos915@163.com>
 */

namespace frontend\models;


use common\models\City;
use common\models\Share;
use yii\base\Model;

class ShareForm extends Model
{

    public $name;
    public $start;
    public $ban_result;
    public $ban_num;
    public $q_result;
    public $q_num;
    public $events;

    public function rules()
    {
        return [
            [['name', 'start', 'q_result', 'ban_result', 'ban_num' , 'q_num'], 'string'],
            ['events', 'safe']
        ];
    }

    public function submit($params)
    {
        $this->load($params, '');
        if (!$this->validate()) {
            return false;
        }
        // 处理城市
        $events = count($this->events);
        // 更改城市数
        City::updateAllCounters(['num' => 1], ['id' => $this->events]);

        $model = new Share();
        $model->name = $this->name;
        $model->start = strtotime($this->start);
        $model->ban_result = $this->ban_result;
        $model->ban_num = $this->ban_num;
        $model->q_result = $this->q_result;
        $model->num = intval($events);
        $model->q_num = $this->q_num;
        $model->created_at = time();

        if (!$model->save()) {
            $this->addError('', '保存失败');
            return false;
        }

        return $model;
    }

}